<?php
class ModelGaraReceipt extends Model
{ 
	private $arr_col = array(
							
							'code',
							'carid',
							'carnumber',
							'manufacturer',
							'model',
							'color',
							'receiptdate',
							'status',
							'customerid',
							'customername',
							'phone',
							'email',
							'address',
							'description',
							'completedate',
							'feename',
							'fee',
							'total',
							'payment',
							'remain'
							);
	public $status = array(
							'new' => "Biên nhận mới",
							'pending' => "Biên nhận đang xử lý",
							'complete' => "Biên nhận đã hoàng thành",
							'payment' => "Biên nhận đã thanh toán",
							'cancel' => "Hủy biên nhận"
							);
	public function getItem($id)
	{
		$query = $this->db->query("Select `gara_receipt`.* 
									from `gara_receipt` 
									where id ='".$id."' ");
		return $query->row;
	}
	
	
	
	public function getList($where="", $from=0, $to=0)
	{
		
		$sql = "Select `gara_receipt`.* 
									from `gara_receipt` 
									where 1=1 " . $where ;
		if($to > 0)
		{
			$sql .= " Limit ".$from.",".$to;
		}
		//echo $sql;
		$query = $this->db->query($sql);
		return $query->rows;
	}
	
	public function updateCol($id,$col,$val)
	{
		$id = $id;
		$col = $col;
		$val = $val;
		
		
		$field=array(
						$col
					);
		$value=array(
						$val
					);
		
		$where="id = '".$id."'";
		$this->db->updateData('gara_receipt',$field,$value,$where);
	}
	
	public function gentCode($prefix)
	{
		$id = $this->db->getNextIdVarCharNumber("gara_receipt","code",$prefix);
		return $prefix.$id;
	}
	
	public function save($data)
	{
		$gara_receipt = $this->getItem($data['id']);
		
		$value = array();
		if(count($gara_receipt))
		{
			foreach($gara_receipt as $col => $val)
			{
				if(isset($data[$col]))
					$gara_receipt[$col] = $data[$col];
			}
			$data = $gara_receipt;
		}
		
		foreach($this->arr_col as $col)
		{
			$value[] = $this->db->escape(@$data[$col]);
		}
		

		$field=$this->arr_col;
		
		if(count($gara_receipt) == 0)
		{
			
			$data['id'] = $this->db->insertData("gara_receipt",$field,$value);
		}
		else
		{
			$where="id = '".$data['id']."'";
			$this->db->updateData("gara_receipt",$field,$value,$where);
		}
		return $data['id'];
	}
	
	public function delete($id)
	{
		$where="id = '".$id."'";
		$this->db->deleteData("gara_receipt",$where);
		
	}
	//Chi tiet receipt
	public function getReceiptDetail($id)
	{
		$sql = "Select `gara_receipt_detail`.*
									from `gara_receipt_detail` 
									where id ='".$id."' ".$where;
		$query = $this->db->query($sql);
		return $query->row;
	}
	public function getReceiptDetailList($where)
	{
		$sql = "Select `gara_receipt_detail`.*
									from `gara_receipt_detail` 
									where 1=1 " . $where;
		
		$query = $this->db->query($sql);
		return $query->rows;
	}
	
	private $arr_coldetail = array(
							'receiptid',
							'mediaid',
							'price',
							'discount',
							'discountpercent',
							'quantity',
							'unit',
							'subtotal'
							);
	public function saveReceiptDetail($data)
	{
		
		$gara_receiptdetail = $this->getReceiptDetail($data['id']);
		
		$value = array();
		if(count($gara_receiptdetail))
		{
			foreach($gara_receiptdetail as $col => $val)
			{
				if(isset($data[$col]))
					$gara_receiptdetail[$col] = $data[$col];
			}
			$data = $gara_receiptdetail;
		}
		
		foreach($this->arr_coldetail as $col)
		{
			$value[] = $this->db->escape(@$data[$col]);
		}
		

		$field=$this->arr_coldetail;
		
		if(count($gara_receiptdetail) == 0)
		{
			$data['id'] = $this->db->insertData("gara_receipt_detail",$field,$value);
		}
		else
		{
			$where="id = '".$data['id']."'";
			$this->db->updateData("gara_receipt_detail",$field,$value,$where);
		}
		return $data['id'];
	}
	public function deleteReceiptDetail($id)
	{
		$where="id = '".$id."'";
		$this->db->deleteData("gara_receipt_detail",$where);
	}
}
?>